#include "itemchestentity.h"

#include "application.h"
#include "serviceentity.h"
#include "graphics/graphicsmanager.h"

#include "graphics/texturemanager.h"
#include "graphics.opengl/texture.h"
#include "graphics/texture.h"

#include "application.h"


namespace infinity {

	/*virtual*/ void ItemChestEntity::renderDebug()
	{
		super::renderDebug();


		dw::graphics::Renderer& renderer(dw::graphics::GraphicsManager::getInstance().getRenderer());
		const Matrix4f& objectMtx = m_Transform;

		renderer.pushState();

		renderer.setRenderState(dw::graphics::Renderer::RS_DEPTH_TEST, true);

		renderer.setMatrixMode(dw::graphics::Renderer::MODELVIEW);
		renderer.pushMatrix();
		renderer.multMatrix(objectMtx);

		dw::graphics::ITexture* tex = dw::graphics::TextureManager::getInstance().loadTexture("terrain.png");
		renderer.setTexture(tex);
		renderer.setRenderState(dw::graphics::Renderer::RS_LIGHTING, false);

		dw::maths::Vector2f upleft(6/16.0f,9/16.f);
		float sizecell = 1/16.0f;

		glBegin(GL_QUADS);
		glColor4f(1.0f,1.0f,1.0f,0.3f);
		// Front Face
		glNormal3f( 0.0f, 0.0f, 1.0f);
		glTexCoord2f(upleft.x, upleft.y + sizecell); glVertex3f(0.0f, 0.0f,  1.f);	// Bottom Left Of The Texture and Quad
		glTexCoord2f(upleft.x + sizecell, upleft.y + sizecell); glVertex3f( 1.0f, 0.f,  1.0f);	// Bottom Right Of The Texture and Quad
		glTexCoord2f(upleft.x + sizecell, upleft.y ); glVertex3f( 1.0f,  1.0f,  1.0f);	// Top Right Of The Texture and Quad
		glTexCoord2f(upleft.x, upleft.y ); glVertex3f(0.f,  1.0f,  1.0f);	// Top Left Of The Texture and Quad
		// Back Face
		glNormal3f( 0.0f, 0.0f,-1.0f);
		glTexCoord2f(upleft.x + sizecell, upleft.y + sizecell); glVertex3f(0.f, 0.f, 0.f);	// Bottom Right Of The Texture and Quad
		glTexCoord2f(upleft.x + sizecell, upleft.y ); glVertex3f(0.f,  1.0f, 0.f);	// Top Right Of The Texture and Quad
		glTexCoord2f(upleft.x , upleft.y ); glVertex3f( 1.0f,  1.0f, 0.f);	// Top Left Of The Texture and Quad
		glTexCoord2f(upleft.x , upleft.y + sizecell); glVertex3f( 1.0f, 0.f, 0.f);	// Bottom Left Of The Texture and Quad


		upleft = dw::maths::Vector2f(5/16.0f,9/16.f);

		// Top Face
		glNormal3f( 0.0f, 1.0f, 0.0f);
		glTexCoord2f(upleft.x + sizecell, upleft.y ); glVertex3f(0.f,  1.0f, 0.f);	// Top Left Of The Texture and Quad
		glTexCoord2f(upleft.x , upleft.y ); glVertex3f(0.f,  1.0f,  1.0f);	// Bottom Left Of The Texture and Quad
		glTexCoord2f(upleft.x , upleft.y + sizecell); glVertex3f( 1.0f,  1.0f,  1.0f);	// Bottom Right Of The Texture and Quad
		glTexCoord2f(upleft.x + sizecell, upleft.y + sizecell); glVertex3f( 1.0f,  1.0f, 0.f);	// Top Right Of The Texture and Quad
		// Bottom Face
		glNormal3f( 0.0f,-1.0f, 0.0f);
		glTexCoord2f(upleft.x + sizecell, upleft.y + sizecell); glVertex3f(0.f, 0.f, 0.f);	// Top Right Of The Texture and Quad
		glTexCoord2f(upleft.x , upleft.y + sizecell); glVertex3f( 1.0f, 0.f, 0.f);	// Top Left Of The Texture and Quad
		glTexCoord2f(upleft.x , upleft.y ); glVertex3f( 1.0f, 0.f,  1.0f);	// Bottom Left Of The Texture and Quad
		glTexCoord2f(upleft.x + sizecell, upleft.y); glVertex3f(0.f, 0.f,  1.0f);	// Bottom Right Of The Texture and Quad

		upleft = dw::maths::Vector2f(7/16.0f,9/16.f);
		// Right face
		glNormal3f( 1.0f, 0.0f, 0.0f);
		glTexCoord2f(upleft.x + sizecell, upleft.y + sizecell); glVertex3f( 1.0f, 0.f, 0.f);	// Bottom Right Of The Texture and Quad
		glTexCoord2f(upleft.x + sizecell, upleft.y ); glVertex3f( 1.0f,  1.0f, 0.f);	// Top Right Of The Texture and Quad
		glTexCoord2f(upleft.x , upleft.y); glVertex3f( 1.0f,  1.0f,  1.0f);	// Top Left Of The Texture and Quad
		glTexCoord2f(upleft.x , upleft.y + sizecell); glVertex3f( 1.0f, 0.f,  1.0f);	// Bottom Left Of The Texture and Quad
		// Left Face
		glNormal3f(-1.0f, 0.0f, 0.0f);
		glTexCoord2f(upleft.x , upleft.y + sizecell); glVertex3f(0.f, 0.f, 0.f);	// Bottom Left Of The Texture and Quad
		glTexCoord2f(upleft.x + sizecell, upleft.y+ sizecell); glVertex3f(0.f, 0.f,  1.0f);	// Bottom Right Of The Texture and Quad
		glTexCoord2f(upleft.x + sizecell, upleft.y ); glVertex3f(0.f,  1.0f,  1.0f);	// Top Right Of The Texture and Quad
		glTexCoord2f(upleft.x , upleft.y ); glVertex3f(0.f,  1.0f, 0.f);	// Top Left Of The Texture and Quad
		glEnd();

		renderer.popState();
		renderer.popMatrix();

		if(m_open)
		{
			const dw::system::Window& window = Application::getInstance().getWindow();

			renderer.pushState();
			renderer.pushMatrix();
			RECT rect;								
			GetClientRect (window.getHandle(),&rect);		
			renderer.setMatrixMode(dw::graphics::Renderer::PROJECTION);

			renderer.pushMatrix();
			renderer.loadIdentity();

			renderer.setOrtho2D(0,rect.right,0,rect.bottom,-1,1);				
			renderer.setMatrixMode(dw::graphics::Renderer::MODELVIEW);						

			renderer.setRenderState(dw::graphics::Renderer::RS_DEPTH_TEST, true);
			renderer.setRenderState(dw::graphics::Renderer::RS_LIGHTING, false);
			renderer.setRenderState(dw::graphics::Renderer::RS_ALPHA_TEST, true);

			//UI 
			renderer.loadIdentity();

			dw::graphics::ITexture* terrain = dw::graphics::TextureManager::getInstance().getTexture("terrain.png");
			renderer.setTexture(terrain);

			if(m_pInventaire)
			{
				m_pInventaire->updateGUI();

				renderer.setVertexBuffer(m_pInventaire->getVertexBuffer());
				renderer.setIndexBuffer(m_pInventaire->getIndexBuffer());
				renderer.drawIndexedPrimitive(dw::graphics::primitive::triangles, 0, m_pInventaire->getTrianglesCount() * 3);
			}

			renderer.setMatrixMode(dw::graphics::Renderer::PROJECTION);					
			renderer.popMatrix();	
			renderer.setMatrixMode(dw::graphics::Renderer::MODELVIEW);
			renderer.popMatrix();		
			renderer.popState();
		}
	}

	ItemChestEntity::ItemChestEntity()
	:	super()
	, m_open(false)
	{
		m_pContainer = new Container(30);

		Object obj;
		for(unsigned int index=0; index < 10; ++index)
		{
			obj.type = Object::BLOC;
			obj.id = index;

			m_pContainer->add(obj,1);
		}
	
		m_pInventaire = new render::Inventaire(m_pContainer);

		m_pInventaire->initializeGUI();
	}

	/*virtual*/ ItemChestEntity::~ItemChestEntity()
	{
		delete m_pInventaire;
		delete m_pContainer;
	}


	/*virtual*/ void ItemChestEntity::leftClick()
	{
		dwLogDebug("chest Box LeftClick !!! \n");
	}
	/*virtual*/ void ItemChestEntity::rightClick()
	{
		dwLogDebug("chest Box RightClick !!! \n");
		m_open = !m_open;
	}


} // namespace infinity
